 /*   
 * Project: OSMP
 * FileName: SceneController.java
 * version: V1.0
 */
package com.cnpc.iot.scene.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.SocketUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cnpc.framework.annotation.RefreshCSRFToken;
import com.cnpc.framework.base.entity.Org;
import com.cnpc.framework.base.entity.SysFile;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.base.service.OrgService;
import com.cnpc.iot.scene.entity.Device;

/**
 * Description:场景设置
 * @author: wangkaiping
 * @date: 2018年2月10日 下午4:46:37上午10:51:30
 */
@Controller
@RequestMapping("/scene")
public class SceneController {
	
	@Resource
    private BaseService baseService;
	
	@Resource
    private OrgService orgService;
	
	@RefreshCSRFToken
    @RequestMapping(value="/addimg",method = RequestMethod.GET)
    public String addimg(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scene/addimg";
    }
    
    @RefreshCSRFToken
    @RequestMapping(value="/showImage/{id}",method = RequestMethod.GET)
    public String showimg(@PathVariable("id") String id,HttpServletRequest request){
    	Org org = baseService.get(Org.class, id);
        request.setAttribute("org",org);
        return "iot/scene/showimg";
    }
    
    @RequestMapping(value="/saveImage/{id}/{fileIds}",method = RequestMethod.POST)
    @ResponseBody
    public Result saveImage(@PathVariable("id") String id, @PathVariable("fileIds") String fileIds, HttpServletRequest request){
    	Org org = baseService.get(Org.class, id);
    	SysFile file = baseService.get(SysFile.class, fileIds);
    	org.setImages(file.getFilePath().replaceAll("\\\\", "/"));
        baseService.update(org);
        return new Result(true);
    }
    
    @RequestMapping(value="/saveDevice/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result saveDevice(@PathVariable("orgId") String orgId, String obj, HttpServletRequest request){
    	Org org = baseService.get(Org.class, orgId);
    	List<Device> devices = JSONObject.parseArray(obj, Device.class);
    	for(Device i: devices){
    		i.setId(UUID.randomUUID().toString());
    		i.setOrgid(orgId);
    		i.setLevelCode(org.getLevelCode());
    		i.setCreateDateTime(new Date());
    		i.setUpdateDateTime(new Date());
    		i.setVersion(0);
    		i.setDeleted(0);
    	}
    	List<Device> temps = baseService.find("from Device where orgid='"+orgId+"'");
    	baseService.batchDelete(temps);
    	baseService.batchSave(devices);
        return new Result(true);
    }
    
    @RequestMapping(value="/device_list",method = RequestMethod.GET)
    public String deviceList(){
        return "iot/scene/device_list";
    }
    
    @RequestMapping(value="/getMarkData/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result getMarkData(@PathVariable("orgId") String orgId){
    	Org org = baseService.get(Org.class, orgId);
    	List<Device> devices = baseService.find("from Device where orgid='"+orgId+"'");
    	Map<String,Object> object = new HashMap<>();
    	object.put("org", org);
    	object.put("devices", devices);
    	Result result = new Result(true);
    	result.setData(object);
    	return result;
    }
    
    @RequestMapping(value="/lookupData/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result lookupData(@PathVariable("orgId") String orgId){
    	List<Device> devices = baseService.find("from Device where orgid='"+orgId+"'");
    	List<Map<String,Object>> temp = new ArrayList<>();
    	Random random1 = new Random(100);//随机测试
    	for(Device i : devices){
    		Map<String,Object> object = new HashMap<>();
    		object.put("key", i.getId());
    		object.put("value", random1.nextInt());
    		temp.add(object);
    	}
    	Result result = new Result(true);
    	result.setData(temp);
    	return result;
    }

}
